package com.xuecheng.content.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.BindTeachplanMediaDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.domain.TeachplanMedia;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanMediaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.media.api.MediaClient;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-12-03
 */
@Service
public class TeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements TeachplanMediaService {

    @Autowired
    private TeachplanService teachplanService;//课程计划


    @Autowired
    private CourseBaseService courseBaseService;//课程基本信息

    @Autowired
    private MediaClient mediaClient;//Feign

    //课程计划绑定媒资
    @Override
    public ResponseResult association(BindTeachplanMediaDto bindDto) {
        //前端参数校验
        if (ObjectUtil.isEmpty(bindDto.getMediaId()) || ObjectUtil.isEmpty(bindDto.getTeachplanId())){
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        //查询课程计划
        Teachplan teachplan = teachplanService.getById(bindDto.getTeachplanId());
        //根据课程计划获取课程基本信息
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        //校验课程状态 已发布、审核已通过、已提交的不允许再绑定
        if (StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_COMMIT_STATUS)
                || StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PASTED_STATUS)
                ||  StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.MEDIABINDERROR);
        }
        //远程调用获取媒资信息
        ResponseResult<MediaVo> responseResult = mediaClient.findById(bindDto.getMediaId());
        //判断是否调用成功
        if (responseResult.getCode()!=0){
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        //获取媒资对象
        MediaVo mediaVo = responseResult.getData();
        //封装媒资绑定实体
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setMediaId(mediaVo.getId()); // 媒资id
        teachplanMedia.setTeachplanId(teachplan.getId()); // 计划id
        teachplanMedia.setCourseId(courseBase.getId());// 课程id
        teachplanMedia.setMediaFilename(mediaVo.getFilename()); // 媒资名称
        teachplanMedia.setCreateDate(new Date()); // 创建时间
        teachplanMedia.setCreatePeople(AuthInfoHolder.getUserId().toString());// 创建人
        //保存数据库
        this.save(teachplanMedia);
        //返回结果
        return ResponseResult.okResult();
    }

    //删除课程计划绑定的媒资
    @Override
    public ResponseResult deleteMedia(Long teachplanMediaId) {
        //前端参数校验
        if (teachplanMediaId==null){
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        //查询课程计划
        TeachplanMedia teachplanMedia = this.getById(teachplanMediaId);
        //根据课程计划获取课程基本信息
        CourseBase courseBase = courseBaseService.getById(teachplanMedia.getCourseId());
        //校验课程状态 已发布、审核已通过、已提交的不允许删除
        if (StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_COMMIT_STATUS)
                || StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PASTED_STATUS)
                ||  StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.MEDIABINDERROR);
        }
        //删除绑定的媒资信息
        this.removeById(teachplanMediaId);
        //返回结果
        return ResponseResult.okResult();
    }
}
